package com.hzo.great.leisuregreat.views;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.base.BaseActivity;
import com.hzo.great.leisuregreat.constants.UrlConstant;
import com.hzo.great.leisuregreat.dialog.LoadingDialog;
import com.hzo.great.leisuregreat.utils.MyStringCallback;
import com.hzo.great.leisuregreat.utils.RequestSever;
import com.hzo.great.leisuregreat.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

public class ModifyPasswordActivity extends BaseActivity {

    ImageView close;

    EditText phoneNum, verifyCode, password;

    TextView getVerifyCode, modifyPwd;

    String phoneNumber, password1, verifyCode1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fullScreen(this, true);
        setContentView(R.layout.modify_pwd);
        initView();
    }

    private void initView() {
        close = (ImageView) findViewById(R.id.close);

        verifyCode = (EditText) findViewById(R.id.verifyCode);
        password = (EditText) findViewById(R.id.password);
        phoneNum = (EditText) findViewById(R.id.phoneNum);

        getVerifyCode = (TextView) findViewById(R.id.getVerifyCode);
        modifyPwd = (TextView) findViewById(R.id.modifyPwd);

        close.setOnClickListener(this);
        getVerifyCode.setOnClickListener(this);
        modifyPwd.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.getVerifyCode:
                phoneNumber = phoneNum.getText().toString();
                if (phoneNumber.equals("") || !phoneNumber.startsWith("1") || phoneNumber.length() != 11){
                    ShortToast("请输入正确手机号");
                    return;
                }
                getVerifyCode();
                break;
            case R.id.modifyPwd:
                phoneNumber = phoneNum.getText().toString();
                password1 = password.getText().toString();
                verifyCode1 = verifyCode.getText().toString();

                if (phoneNumber.equals("") || !phoneNumber.startsWith("1") || phoneNumber.length() != 11){
                    ShortToast("请输入正确手机号");
                    return;
                }

                if (password1.equals("")){
                    ShortToast("请输入密码");
                    return;
                }


                if (password1.length() < 6){
                    ShortToast("密码不能低于6位");
                    return;
                }

                if (verifyCode1.equals("")){
                    ShortToast("必须填写验证码");
                    return;
                }
                if (verifyCode1.length() == 4 || verifyCode1.length() == 6){
                    modifyPwd();
                }else {
                    ShortToast("请输入正确验证码");
                    return;
                }

                break;
        }
    }

    private void modifyPwd() {
        Map<String, Object> map = new HashMap<>();
        map.put("PhoneNo", phoneNumber);
        map.put("newPwd",password1);
        map.put("validateCode",verifyCode1);
        LoadingDialog.getInstance(this).showLoadDialog("");
        RequestSever.post(UrlConstant.MODIFYPWD_URL,map,new MyStringCallback<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LoadingDialog.closeLoadDialog();
                JSONObject jsonObject = JSON.parseObject(result);
                switch (jsonObject.getIntValue("code")){
                    case 200:
                        SharedPreferencesUtils.removeAll();
                        ActivityCollector.removeAllActivity();
                        startActivity(new Intent(ModifyPasswordActivity.this,LoginActivity.class));
                        finish();
                        break;

                        default:
                            ShortToast(jsonObject.getString("message"));
                            break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LoadingDialog.closeLoadDialog();
            }
        });
    }

    public void ShortToast(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    public void getVerifyCode() {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phoneNumber);
        map.put("type",2);
        RequestSever.post(UrlConstant.SENDSMS_URL,map,new MyStringCallback<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = JSON.parseObject(result);
                ShortToast(jsonObject.getString("message"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
}
